package com.woniu113.junk.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu113.junk.user.Param.addressInfo.DefaultAddressParam;
import com.woniu113.junk.user.Param.addressInfo.ModifyAddressParam;
import com.woniu113.junk.user.Param.addressInfo.UserAddressInfoParam;
import com.woniu113.junk.user.dto.address.FindAddressDTO;
import com.woniu113.junk.user.exception.UserException;
import com.woniu113.junk.user.model.AddressInfo;
import com.woniu113.junk.user.mapper.AddressInfoMapper;
import com.woniu113.junk.user.service.AddressInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@Service
public class AddressInfoServiceImpl extends ServiceImpl<AddressInfoMapper, AddressInfo> implements AddressInfoService {
    @Resource
    private AddressInfoMapper addressInfoMapper;
    @Override
    /*添加地址*/
    public void userAddressInfo(UserAddressInfoParam param) {
        /*查出本人是否存在地址*/
        QueryWrapper<AddressInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",param.getUserId()).eq("address_state",0);
        List<AddressInfo> addressInfos = addressInfoMapper.selectList(queryWrapper);
        AddressInfo addressInfo = BeanUtil.toBean(param, AddressInfo.class);
        /*如果存在则设置为非默认地址*/
        if (addressInfos.size()>0){
            addressInfo.setAddressState(0).setType(1);
        }else if (addressInfos.size()==5){
            throw new UserException("最多只能添加5个地址",2001);
        } else{
            addressInfo.setAddressState(0).setType(0);
        }
        save(addressInfo);
    }

    @Override
    /*设置默认地址*/
    public void defaultAddress(DefaultAddressParam param) {
        //查询出先前的默认地址
        QueryWrapper<AddressInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",param.getUserId()).eq("type",0);
        AddressInfo addressInfo = getOne(queryWrapper);
        /*查询该用户有没有地址*/
        if (addressInfo==null)throw new UserException("该用户没有默认地址",2000);
        if (addressInfo.getAddressId()==param.getAddressId())throw new UserException("该地址已经是默认地址",2001);
        addressInfo.setType(1);
        /*查询要修改的地址是否正常*/
        AddressInfo addressInfo1 = addressInfoMapper.selectById(param.getAddressId());
        if (addressInfo1==null)throw new UserException("该地址不存在",2002);
        if (addressInfo1.getUserId()!=param.getUserId())throw new UserException("该地址不是本人地址",2002);
        if (addressInfo1.getAddressState()==1)throw new UserException("该地址已删除",2003);
        addressInfo1.setType(0);
        /*修改的地址*/
        updateById(addressInfo);
        updateById(addressInfo1);
    }

    @Override
    /*删除地址*/
    public void deleteAddress(DefaultAddressParam param) {
        /*查询地址是否存在*/
        AddressInfo addressInfo1 = addressInfoMapper.selectById(param.getAddressId());
        if (addressInfo1==null)throw new UserException("该地址不存在",2002);
        if (addressInfo1.getUserId()!=param.getUserId())throw new UserException("该地址不是本人地址",2002);
        if (addressInfo1.getAddressState()==1)throw new UserException("该地址已删除",2003);
        /*修改地址状态*/
        addressInfo1.setAddressState(1);
        addressInfo1.setType(1);
        updateById(addressInfo1);
        /*判断还有没有其他地址随机一个作为默认地址*/
        QueryWrapper<AddressInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",param.getUserId()).eq("address_state",0);
        List<AddressInfo> addressInfos = addressInfoMapper.selectList(queryWrapper);
        /*随机一个作为默认地址*/
        if (addressInfos.size()>0){
            AddressInfo addressInfo = addressInfos.get(0);
            addressInfo.setType(0);
            updateById(addressInfo);
        }
    }

    @Override
    /*根据用户查询自己默认地址*/
    public AddressInfo defaultAddressById(int userId) {
        QueryWrapper<AddressInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("type",0);
        return getOne(queryWrapper);
    }

//    @Override
//    /*根据用户查询所有地址*/
//    public List<AddressInfo> addressByUserId(int userId) {
//        QueryWrapper<AddressInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("user_id",userId).eq("address_state",0);
//        return  addressInfoMapper.selectList(queryWrapper);
//    }

    @Override
    /*用户修改自己地址信息*/
    public void modifyAddress(ModifyAddressParam param) {
        /*找出地址进行判断*/
        AddressInfo addressInfo = addressInfoMapper.selectById(param.getAddressId());
        if (addressInfo==null)throw new UserException("该地址不存在",2002);
        if (addressInfo.getUserId()!=param.getUserId())throw new UserException("该地址不是本人地址",2002);
        if (addressInfo.getAddressState()==1)throw new UserException("该地址已删除",2003);
        /*修改地址*/
        addressInfo.setAddressName(param.getAddressName()).setCity(param.getCity()).setCounty(param.getCounty()).setLongitude(param.getLongitude()).setLatitude(param.getLatitude());
        updateById(addressInfo);
    }

    @Override
    public FindAddressDTO addressByUserId(int userId, int offset, int pageSize) {
        QueryWrapper<AddressInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("address_state", 0);
        int size = addressInfoMapper.selectList(queryWrapper).size();
        queryWrapper.last("LIMIT " + offset + ", " + pageSize);
        List<AddressInfo> infos = addressInfoMapper.selectList(queryWrapper);
        FindAddressDTO dto = new FindAddressDTO();
        dto.setList(infos);
        dto.setTotal(size);
        return dto;
    }
}
